package Component

import (
	"bufio"
	"fmt"
	"os/exec"
)

type LocalShellComponent struct {
	cmd string
}

func (self *LocalShellComponent) Run() (err error) {
	fmt.Println(self.cmd)
	cmd := exec.Command(self.cmd)

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		fmt.Println(err)
		return
	}
	cmd.Start()
	in := bufio.NewScanner(stdout)
	for in.Scan() {
		cmdRe:=ConvertByte2String(in.Bytes(),"GB18030")
		fmt.Println(cmdRe)
	}
	cmd.Wait()

	//
	//piper, _ := cmd.StdoutPipe()
	//cmd.Start()
	//reader := bufio.NewReader(piper)
	//for {
	//	line, err := reader.ReadString('\n')
	//
	//	if line != "" {
	//		fmt.Println(strings.TrimSpace(line))
	//	}
	//
	//	if err != nil {
	//		if err != io.EOF {
	//			fmt.Println(err)
	//		}
	//
	//		break
	//	}
	//}

	return
}

func (self *LocalShellComponent) SetParameter(name string, parameter string) {
	if name == CMD {
		self.cmd = parameter
	}
}
